package com.ccom.test.controller;

import com.ccom.core.common.constant.ServiceNameConstants;
import com.ccom.core.common.model.Result;
import com.ccom.core.common.util.StringUtils;
import com.ccom.test.constant.TestConstant;
import com.ccom.test.service.RabbitMqTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: rabbitmq测试
 * @author: jjzhang24
 * @date: 2021年12月17日
 */
@Slf4j
@Api(tags = "rabbitmq测试")
@RestController
@RequestMapping(ServiceNameConstants.TEST_SERVICE_URL_PREFIX + "/rabbitmq")
public class RabbitMqTestController {

    @Autowired
    private RabbitMqTestService rabbitMqTestService;

    @GetMapping("/sendMessage")
    @ApiOperation(value = "发送消息到rabbitmq指定队列", notes = "queue为空时默认为test-queue队列")
    public Result sendMessage(@RequestParam(name = "queue", required = false) String queue, @RequestParam("message") String meassage) {
        if (StringUtils.isEmpty(queue)) {
            queue = TestConstant.RABBIT_TEST_QUEUQ;
        }
        rabbitMqTestService.sendMassage(queue, meassage);
        return Result.success();
    }

    @GetMapping("/sendMessageAllTime")
    @ApiOperation(value = "连续发送10次消息到rabbitmq指定队列", notes = "queue为空时默认为test-queue队列")
    public Result sendMessageAllTime(@RequestParam(name = "queue", required = false) String queue, @RequestParam("message") String meassage) {
        if (StringUtils.isEmpty(queue)) {
            queue = TestConstant.RABBIT_TEST_QUEUQ;
        }
        for (int i = 0; i < 10; i++) {
            rabbitMqTestService.sendMassage(queue, meassage);
        }
        return Result.success();
    }

    @GetMapping("/sendMessageDelay")
    @ApiOperation(value = "延迟发送消息到rabbitmq指定队列", notes = "queue为空时默认为test-queue-delay队列,time为空时默认为5秒")
    public Result sendMessageDelay(@RequestParam(name = "queue", required = false) String queue, @RequestParam(name = "time", required = false) Integer time, @RequestParam("message") String meassage) {
        if (StringUtils.isEmpty(queue)) {
            queue = TestConstant.RABBIT_TEST_QUEUQ_DELAY;
        }
        if (time == null) {
            time = 5;
        }
        rabbitMqTestService.sendMassage(queue, meassage, time);
        return Result.success();
    }


    @GetMapping("/publishEvent")
    @ApiOperation("发送远程事件")
    public Result publishEvent(@RequestParam("message") String meassage) {
        rabbitMqTestService.publishEvent(meassage);
        return Result.success();
    }

    @GetMapping("/receiveFromQueue")
    @ApiOperation("接收消息")
    public Result receiveFromQueue(@RequestParam("queueName") String queueName) {
        String result = rabbitMqTestService.receiveFromQueue(queueName);
        return Result.success(result);
    }

    @GetMapping("/subcribe")
    @ApiOperation("测试订阅文章")
    public Result subcribe(@RequestParam("articleId") String articleId, @RequestParam("userId") String userId) {
        boolean result = rabbitMqTestService.subcribe(articleId, userId);
        return Result.date(result);
    }
}
